package vcp.client.GUI.factories;

import java.util.EnumMap;

import vcp.client.GUI.mvc.controller.UIAbstractController;
import vcp.client.GUI.mvc.controller.UIAccStatusController;
import vcp.client.GUI.mvc.controller.UIApproveCostsController;
import vcp.client.GUI.mvc.controller.UICheckInController;
import vcp.client.GUI.mvc.controller.UICheckoutController;
import vcp.client.GUI.mvc.controller.UICompHandleController;
import vcp.client.GUI.mvc.controller.UIDemoController;
import vcp.client.GUI.mvc.controller.UIFillComplaintController;
import vcp.client.GUI.mvc.controller.UIInfoRetrieveController;
import vcp.client.GUI.mvc.controller.UILoginController;
import vcp.client.GUI.mvc.controller.UIOnSiteController;
import vcp.client.GUI.mvc.controller.UIQuarterReportsController;
import vcp.client.GUI.mvc.controller.UIRegularCheckInController;
import vcp.client.GUI.mvc.controller.UISiteMapController;
import vcp.client.GUI.mvc.controller.UIStatisticsController;
import vcp.client.GUI.mvc.controller.UISubRootController;
import vcp.client.GUI.mvc.controller.UISubscriptionController;
import vcp.client.GUI.mvc.controller.UIUpdateCostsController;
import vcp.client.GUI.mvc.model.UIAccStatusModel;
import vcp.client.GUI.mvc.model.UIApproveCostsModel;
import vcp.client.GUI.mvc.model.UICheckInModel;
import vcp.client.GUI.mvc.model.UICheckoutModel;
import vcp.client.GUI.mvc.model.UICompHandleModel;
import vcp.client.GUI.mvc.model.UIFillComplaintModel;
import vcp.client.GUI.mvc.model.UIInfoRetrieveModel;
import vcp.client.GUI.mvc.model.UILoginModel;
import vcp.client.GUI.mvc.model.UIQuarterReportsModel;
import vcp.client.GUI.mvc.model.UIRootModel;
import vcp.client.GUI.mvc.model.UISiteMapModel;
import vcp.client.GUI.mvc.model.UIStatisticsModel;
import vcp.client.GUI.mvc.model.UISubscriptionModel;
import vcp.client.GUI.mvc.model.UIUpdateCostsModel;
import vcp.client.GUI.mvc.view.UIAccStatusView;
import vcp.client.GUI.mvc.view.UIApproveCostsView;
import vcp.client.GUI.mvc.view.UICheckInView;
import vcp.client.GUI.mvc.view.UICheckoutView;
import vcp.client.GUI.mvc.view.UICompHandleView;
import vcp.client.GUI.mvc.view.UIDemoView;
import vcp.client.GUI.mvc.view.UIFillComplaintView;
import vcp.client.GUI.mvc.view.UIInfoRetrieveView;
import vcp.client.GUI.mvc.view.UILoginView;
import vcp.client.GUI.mvc.view.UIOnSiteView;
import vcp.client.GUI.mvc.view.UIQuarterReportsView;
import vcp.client.GUI.mvc.view.UIRegularCheckInView;
import vcp.client.GUI.mvc.view.UISiteMapView;
import vcp.client.GUI.mvc.view.UIStatisticsView;
import vcp.client.GUI.mvc.view.UISubRootView;
import vcp.client.GUI.mvc.view.UISubscriptionView;
import vcp.client.GUI.mvc.view.UIUpdateCostsView;
import vcp.client.interfaces.delegates.IParentControllerDelegate;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         Control over all the factory
 *         </p>
 * @see UIRootModel
 * @see EnumMap
 * @see UIAbstractController
 */
public class MVCFactory
{
	/** reference to main model **/
	private final UIRootModel mainModel;

	/** Map that hold the controllers **/
	private final EnumMap <Screen, UIAbstractController> controllerMap;

	public MVCFactory ( UIRootModel model )
	{
		this.mainModel = model;

		this.controllerMap = new EnumMap <>(Screen.class);
	}

	// ****************************************************************************************************************************
	// Public methods
	// ****************************************************************************************************************************
	/**
	 * Controller creation
	 * 
	 * @param screenName
	 * @param parent
	 * @return UIAbstractController
	 */
	public UIAbstractController createController ( Screen screenName, IParentControllerDelegate parent )
	{
		if ( ! this.controllerMap.containsKey(screenName) )
		{
			try
			{
				UIAbstractController createdState = this.makeController(screenName);
				createdState.setParentControllerDelegate(parent);
				this.controllerMap.put(screenName, createdState);
			}
			catch ( NullPointerException e )
			{
				return null;
			}
		}

		return this.controllerMap.get(screenName);
	}

	// ****************************************************************************************************************************
	// Private methods
	// ****************************************************************************************************************************
	/**
	 * update the controller with data
	 * 
	 * @param screenName
	 * @return UIAbstractController
	 */
	private UIAbstractController makeController ( Screen screenName )
	{
		UIAbstractController createdState = null;
		/** check the status and update according to the status **/
		switch ( screenName )
		{
			case DEMO:
				createdState = new UIDemoController(new UIDemoView());
				break;
			case LOGIN:
				createdState = new UILoginController(new UILoginView(), new UILoginModel(this.mainModel));
				break;
			case ONSITE:
				createdState = new UIOnSiteController(new UIOnSiteView());
				break;
			case CHECK_IN:
				createdState = new UICheckInController(new UICheckInView(), new UICheckInModel(this.mainModel));
				break;
			case REGULAR_CHECK_IN:
				createdState = new UIRegularCheckInController(new UIRegularCheckInView(), new UICheckInModel(this.mainModel));
				break;
			case CHECK_OUT:
				createdState = new UICheckoutController(new UICheckoutView(), new UICheckoutModel(this.mainModel));
				break;
			case WORKER:
			case CUSTOMER:
				createdState = new UISubRootController(new UISubRootView(), new UILoginModel(this.mainModel), this);
				break;
			case SUBSCRIBER:
				createdState = new UISubscriptionController(new UISubscriptionView(), new UISubscriptionModel(this.mainModel));
				break;
			case ACCOUNT_STATUS:
				createdState = new UIAccStatusController(new UIAccStatusView(), new UIAccStatusModel(this.mainModel));
				break;
			case FILL_COMPLAINT:
				createdState = new UIFillComplaintController(new UIFillComplaintView(), new UIFillComplaintModel(this.mainModel));
				break;
			case SITE_MAP:
				createdState = new UISiteMapController(new UISiteMapView(), new UISiteMapModel(this.mainModel));
				break;
			case COMPLAINT_HANDLE:
				createdState = new UICompHandleController(new UICompHandleView(), new UICompHandleModel(this.mainModel));
				break;
			case UPDATE_COSTS:
				createdState = new UIUpdateCostsController(new UIUpdateCostsView(), new UIUpdateCostsModel(this.mainModel));
				break;
			case APPROVE_COSTS:
				createdState = new UIApproveCostsController(new UIApproveCostsView(), new UIApproveCostsModel(this.mainModel));
				break;
			case GEN_REPORTS:
				createdState = new UIQuarterReportsController(new UIQuarterReportsView(), new UIQuarterReportsModel(this.mainModel));
				break;
			case INFORMATION:
				createdState = new UIInfoRetrieveController(new UIInfoRetrieveView(), new UIInfoRetrieveModel(this.mainModel));
				break;
			case STATISTICS:
				createdState = new UIStatisticsController(new UIStatisticsView(), new UIStatisticsModel(this.mainModel));
			default:
				break;
		}

		return createdState;
	}
}
